package ar.org.cfired.creditos.gui;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
 * Contiene datos de una solapa de presentaci&oacute;n
 * @author Rogelio Di Pasquale - Revolution BPM
 * */
public class Solapa {
	private String nombre;
	private String descripcion;
	private String onLoad;
	private int orden;
	private ArrayList<String> includesURL;
	private ArrayList<String> includesTipo;
	
	
	
	public Solapa() {
		nombre = null;
		descripcion = null;
		orden = 0;
		includesURL = new ArrayList<String>();
		includesTipo = new ArrayList<String>();
		onLoad = new String();
	}

	public void setNombre (String nombre) {
			this.nombre = nombre;		
	}
	public String getNombre() {
		return (this.nombre);
	}

	public void setDescripcion (String descripcion) {
			this.descripcion = descripcion;
	}
	public String getDescripcion() {
		return (this.descripcion);
	}
	
	public void setOnLoad (String onLoad) {
		this.onLoad = onLoad;		
	}
	public String getOnLoad() {
		return (this.onLoad);
	}

	public void setOrden (int orden) {
		this.orden = orden;
	}
	public int getOrden() {
		return (this.orden);
	}
	
	public void addInclude(String tipo, String URL) {
		includesTipo.add(tipo);
		includesURL.add(URL);
	}
	
	public ArrayList<String> getIncludesTipo() {
		return (this.includesTipo);
	}

	public ArrayList<String> getIncludesURL() {
		return (this.includesURL);
	}
	
	public ArrayList<String> getIncludesURL(String tipo) {
		ArrayList<String> lista = new ArrayList<String>(); 
		for (int i = 0; i < includesURL.size(); i++) {
			if (includesTipo.get(i)!=null)
				if (includesTipo.get(i).equals(tipo))
					lista.add(includesURL.get(i));
		}
		return(lista);
	}
	
	public static String toIncludeHTML (String tipo, String URL) {
		String res = new String();
		if (tipo.equals("css")) res = "<LINK href=\"" + URL + "\" type=text/css rel=stylesheet>";
		if (tipo.equals("js")) res = "<SCRIPT language=javascript src=\"" + URL + "\"></SCRIPT>";
		if (tipo.equals("vbs")) res = "<SCRIPT language=vbscript src=\"" + URL + "\"></SCRIPT>";		
		return(res + "\n");
	}
	
	public String toIncludeHTML() {
		String respuesta = new String();
		for (int i=0; i<includesURL.size(); i++) {
			respuesta += Solapa.toIncludeHTML(includesTipo.get(i), includesURL.get(i));
		}
		return(respuesta);
	}
	
	public InputStream getInputStream() {
		InputStream respuesta;
		try {
			respuesta = this.getClass().getClassLoader().getResourceAsStream(nombre);
		}
		catch (Exception e) {
			System.out.println("Exception - Solapa.getInputStream - " + e.getMessage());
			respuesta = null;
		}		
		return(respuesta);
	}
	
	public String toHTML() {
		String respuesta = new String();
		InputStream input = this.getInputStream();
		if (input==null)
			return(null);
		try {
		InputStreamReader inp = new InputStreamReader(input);
		BufferedReader entrada = new BufferedReader(inp);
		 char[] buf = new char[1024];
	       int numRead=0;
	       while((numRead=entrada.read(buf)) != -1) {
	           respuesta += String.valueOf(buf, 0, numRead);
	           buf = new char[1024];
	       }
		}
		catch (Exception e) {
			System.out.println("Exception - Solapa.toHTML() - " + e.getMessage());
			return (null);
		}
		
		return (respuesta);
	}

	public void parseObject(Object[] obj) {
		// TO DO: clase formulario que lo concentre y modificar contenidoDocumento
		if (obj.length>=6) {
			try {
				if (obj[0]!=null) this.setDescripcion(String.valueOf(obj[0]));
				if (obj[1]!=null) this.setNombre(String.valueOf(obj[1]));
				if (obj[2]!=null && obj[3]!=null) this.addInclude(String.valueOf(obj[2]), String.valueOf(obj[3]));
				if (obj[4]!=null) this.setOrden(Integer.valueOf(String.valueOf(obj[4])).intValue());
				if (obj[5]!=null) this.setOnLoad(String.valueOf(obj[5]));
			}
			catch (Exception e) {	
				System.out.println("Exception - Solapa.parseObject - " + e.getMessage());
			}
		}
	}
}
